package com.gdones.bootserver.bus.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.bootserver.bus.account.dto.Account;
import com.gdones.bootserver.bus.role.dto.RoleUser;
import com.gdones.bootserver.bus.role.dto.RoleUserDTO;
import com.gdones.bootserver.bus.role.dto.RoleUserListVO;
import com.gdones.bootserver.bus.role.mapper.RoleUserMapper;
import com.gdones.bootserver.bus.role.service.IRoleUserService;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyResult;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-03-28
 */
@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements IRoleUserService {

    @Override
    public MyResult pages(Account user, String roleID, MyPageBand pager) {
        MyResult result = new MyResult();

        RoleUserMapper roleUserMapper = getBaseMapper();

        IPage<RoleUserListVO> pages = roleUserMapper.pages(PageDTO.of(pager.getPage(), pager.getSize()), user, roleID);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("total",pages.getTotal());
        resMap.put("pageData",pages.getRecords());
        resMap.put("page",pager.getPage());
        resMap.put("limit",pager.getSize());

        result.setMsg("查询成功！");
        result.setData(resMap);

        return result;
    }

    @Override
    public MyResult deletes(String ids) {
        MyResult result = new MyResult();

        List<String> idsList = Arrays.asList(ids.split(","));

        RoleUserMapper roleUserMapper = getBaseMapper();

        QueryWrapper<RoleUser> queryWrapper = new QueryWrapper<>();

        queryWrapper.in("infoID",idsList);

        int delete = roleUserMapper.delete(queryWrapper);

        if(delete>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult add(RoleUserDTO roleUserDto) {
        MyResult result = new MyResult();

        List<Account> userList = roleUserDto.getUserList();

        RoleUserMapper roleUserMapper = getBaseMapper();

        //先看是否添加过
        List<Account> isHaveUserList = roleUserMapper.checkUsersIsHave(userList);

        if(isHaveUserList.size()>0){
            String names = "";
            for(Account temp:isHaveUserList){
                names += temp.getUserName()+",";
            }

            names = names.substring(0,names.length() - 1);

            result.setMsg("【"+names+"】用户已授权过角色，请勿重复授权！");
            result.setCode("9999");
            return result;
        }


        boolean isOk = false;

        for(Account user:userList){

            RoleUser roleUserBO = new RoleUser();
            roleUserBO.setRoleID(roleUserDto.getRoleID());
            roleUserBO.setUserID(user.getAccountID());

            int insert = roleUserMapper.insert(roleUserBO);
            if(insert>0){
                isOk = true;
            }else{
                isOk = false;
                break;
            }
        }

        if(isOk){
            result.setMsg("用户授权成功！");
        }else{
            result.setMsg("用户授权出错！");
            result.setCode("9999");
        }

        return result;
    }
}
